import java.util.HashMap;

/**
 * @ClassName Offer50
 * @Description 剑指 Offer 50. 第一个只出现一次的字符
 * @Author clockTown
 * @Date 2021/8/16 15:48
 * @Version 1.0
 */
class Offer50 {
}

/**
 * v0 调用函数
 */
class Offer50V0 {
    public char firstUniqChar(String s) {
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (s.indexOf(ch) == s.lastIndexOf(ch)){
                return ch;
            }
        }
        return ' ';
    }
}

/**
 * v1 hashMap
 */
class Offer50V1 {
    public char firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : chars) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        for (char c : chars) {
            if (map.get(c) == 1){
                return c;
            }
        }

        return ' ';
    }
}